clear
capture log close
set more off
set matsize 8000


**** 

*** Start with city birthplace data
use data_city_birthplace_data, replace

*** Focus on migration within England, Wales and Scotland
drop if location_of_birth=="born at sea"
drop if location_of_birth=="ireland"
drop if location_of_birth=="islands_british"
drop if location_of_birth=="british colonies"
drop if location_of_birth=="foreign"


** Combine Lancashire and Cheshire born population
replace location_of_birth="northwest" if location_of_birth=="lancashire"
replace location_of_birth="northwest" if location_of_birth=="cheshire"


*** Merge in county identifiers
rename location_of_residence loc

sort loc
merge loc using data_regions
tab _merge
keep if _merge==3
drop _merge

replace county="northwest" if county=="cheshire"
replace county="northwest" if county=="lancashire"

*** Drop own-county and cities in the northwest
count
drop if location_of_birth==county
drop if county=="northwest"
drop if loc=="liverpool"
drop if loc=="carlisle"

** Combine non-NW flows
replace location_of_birth="non_nw" if location_of_birth!="northwest"


count
collapse (sum) pop (mean) total_pop, by(location_of_birth loc year)
count

** Reshape to wide
reshape wide pop, i(loc year) j(location_of_birth) string


*** Merge in distance to manchester
sort loc
merge loc using data_dist_to_manchester
tab _merge
keep if _merge==3
drop _merge

** Create distance measure
replace dist=dist/1000
gen dd=.
replace dd=exp(-dist)


*** Merge in textile city indicators
sort loc
merge loc using data_town_cotton_shr_1851
tab _merge
keep if _merge==3
drop _merge

***** Merge in lat and lon data
sort loc
merge loc using data_city_lat_lon_data
tab _merge
keep if _merge==3
drop _merge

rename loc location_of_residence


** Initial level
gen ip=0
replace ip=popnorthwest if year==1851
bysort location_of_residence: egen init_pop=max(ip)
drop ip

gen ip=0
replace ip=total_pop if year==1851
bysort location_of_residence: egen init_tot_pop=max(ip)
replace init_pop=init_pop/init_tot_pop
drop ip init_tot_pop

*** LHS variables
gen ln_pop_nw=ln(popnorthwest)
gen ln_pop_other=ln(popnon_nw)
gen dif_ln_pop_nw=.
sort location_of_residence year
replace dif_ln_pop_nw=ln_pop_nw-ln_pop_nw[_n-1] if location_of_residence==location_of_residence[_n-1]
gen chg_ln_pop_nw=dif_ln_pop_nw-dif_ln_pop_nw[_n-1] if location_of_residence==location_of_residence[_n-1]



*** RHS variables
gen dif_ln_pop_oth=.
sort location_of_residence year
replace dif_ln_pop_oth=ln_pop_oth-ln_pop_oth[_n-1] if location_of_residence==location_of_residence[_n-1]
gen chg_ln_pop_oth=dif_ln_pop_oth-dif_ln_pop_oth[_n-1] if location_of_residence==location_of_residence[_n-1]


gen cotton10=0
replace cotton10=1 if shr_cot>.1

gen shr_tex = shr_wool + shr_other_tex
gen tex10=0
replace tex10=1 if shr_tex>.1

** Distance
replace dd=0 if cotton10==1
gen dd_tex=dd*tex
gen dd_wool=dd*shr_wool

egen dd_std=std(dd)
egen shr_tex_std=std(shr_tex)

encode loc, gen(loc_code)


*** Regressions

* Did outflows to all other cities increase?
reg chg_ln_pop_nw dd_std, robust
est store R1

reg chg_ln_pop_nw shr_tex_std, robust
est store R2

reg chg_ln_pop_nw init_pop, robust
est store R3

reg chg_ln_pop_nw dd_std shr_tex_std init_pop, robust
est store R4

outreg2 [R1 R2 R3 R4] using results_table_7_robust, tex replace see label  drop(_I*)


*** Now with spatial standard errors
gen const=1

ols_spatial_HAC  chg_ln_pop_nw dd_std const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store S1

ols_spatial_HAC  chg_ln_pop_nw shr_tex_std const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store S2

ols_spatial_HAC  chg_ln_pop_nw init_pop const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store S3

ols_spatial_HAC  chg_ln_pop_nw dd_std shr_tex_std init_pop const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store S4

outreg2 [S1 S2 S3 S4] using results_table_7_spatial, tex replace see label  drop(_I*)



